﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using System.Text;
using com.loto.Common;

namespace com.huigo.Admin.Web.customer
{
    public partial class SalesCustList : BasePage
    {
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        private string queryCust
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["cust"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["cust"]);
            }
        }
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["Start"]) ? Convert.ToDateTime(Request.QueryString["Start"]) : DateTime.MinValue;
            }
        }
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["End"]) ? Convert.ToDateTime(Request.QueryString["End"]) : DateTime.MinValue;
            }
        }
        private string queryContact
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["contact"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["contact"]);
            }
        }
        private string queryCompany
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["company"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["company"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string url = "SalesCustList.aspx?";
            url += "page=1";
            if (!string.IsNullOrEmpty(custName.Text))
                url += "&cust=" + custName.Text;
            if (!string.IsNullOrEmpty(beginDate.Text))
                url += "&Start=" + beginDate.Text;
            if (!string.IsNullOrEmpty(endDate.Text))
                url += "&End=" + endDate.Text;
            if (!string.IsNullOrEmpty(contactName.Text))
                url += "&contact=" + contactName.Text;
            if (!string.IsNullOrEmpty(companyName.Text))
                url += "&company=" + companyName.Text;
            if (string.IsNullOrEmpty(custName.Text) && string.IsNullOrEmpty(beginDate.Text) && string.IsNullOrEmpty(endDate.Text) && string.IsNullOrEmpty(contactName.Text) && string.IsNullOrEmpty(companyName.Text))
            {
                url = "SalesCustList.aspx";
            }
            Response.Redirect(url);
        }

        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private string GetPagerUrl()
        {
            string url = "SalesCustList.aspx?";
            url += "page=1";
            if (!string.IsNullOrEmpty(queryCust))
                url += "&cust=" + queryCust;
            if (queryStart!=DateTime.MinValue)
                url += "&Start=" + queryStart;
            if (queryEnd!=DateTime.MinValue)
                url += "&End=" + queryEnd;
            if (!string.IsNullOrEmpty(queryContact))
                url += "&contact=" + queryContact;
            if (!string.IsNullOrEmpty(queryCompany))
                url += "&company=" + queryCompany;
            return url;
        }
        private void BindData()
        {
            custName.Text = queryCust;
            if (queryStart != DateTime.MinValue)
            {
                beginDate.Text = Convert.ToDateTime(queryStart).ToString("yyyy-MM-dd");
            }
            if (queryEnd != DateTime.MinValue)
            {
                endDate.Text = Convert.ToDateTime(queryEnd).ToString("yyyy-MM-dd");
            }
            contactName.Text = queryContact;
            companyName.Text = queryCompany;
            List<RequestFuncParams> list = new List<RequestFuncParams>();
            list.Add(new RequestFuncParams("custName", queryCust));
            list.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            list.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            list.Add(new RequestFuncParams("start", queryStart.ToString()));
            list.Add(new RequestFuncParams("end", queryEnd.ToString()));
            list.Add(new RequestFuncParams("contactName", queryContact));
            list.Add(new RequestFuncParams("companyName", queryCompany));
            BaseService service = new BaseService();
            ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_CUST_LIST_BY_SALES, list);
            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<table>");
                sb.AppendLine("<thead>");
                sb.AppendLine("<tr>");
                sb.AppendLine("<th>");
                sb.AppendLine("经销商");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("EMAIL");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("类型");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("级别");
                sb.AppendLine("</th>");
                sb.AppendLine("<th width=\"12%\">");
                sb.AppendLine("创建时间");
                sb.AppendLine("</th>");
                sb.AppendLine("<th width=\"12%\">");
                sb.AppendLine("最后登录时间");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("状态");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("操作");
                sb.AppendLine("</th>");
                sb.AppendLine("</tr>");
                sb.AppendLine("</thead>");
                sb.AppendLine("<tbody>");

                for (int i = 0; i < parser.RowsCount; i++)
                {
                    sb.AppendLine("<tr>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["CustName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["CustEmail", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["TypeName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["LevelName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(Convert.ToDateTime(parser["CustCreatedOn", i]).ToString("yyyy-MM-dd HH:mm"));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(Convert.ToDateTime(parser["CustLastActivedOn", i]).ToString("yyyy-MM-dd HH:mm"));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictCustStatus, Utils.StrToInt(parser["CustStatus", i], int.MinValue)));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine("<a href=\"AddSalesCust.aspx?cid=" + parser["CustId", i] + "&keepThis=true&amp;TB_iframe=true&amp;height=500&amp;width=850\" title=\"修改经销商-" + parser["CustName", i] + "\" class=\"thickbox\">修改</a>");
                    sb.AppendLine("</td>");
                    sb.AppendLine("</tr>");
                }
                sb.AppendLine("</tbody>");
                sb.AppendLine("</table>");
                litCust.Text = sb.ToString();
            }
            else
            {
                litCust.Text = string.Empty;
            }
        }
    }
}
